// 获取角色id,并发送给后端
$(function () {
    var urlParm = decodeURI(window.location.href.split("?")[1]);
    rId = urlParm.split("roleId=")[1];
    url = '/kobe/modify.do?roleId=' + rId;
    $.get(url, requestFinished);
})

// 修改回显
function requestFinished(data) {
    // console.log(data);
    var rn = data.rName;
    $('#roleName').val(rn);
    // 这里改你数据库读出的
    var arr = data.roleModuleIds;
    // 前后加限定符
    arr = ',' + arr + ',';
    //这里改你checkbox的name值
    var ulli = document.getElementsByName('ulli');
    for (var i = 0; i < ulli.length; i++) {
        if (arr.indexOf(',' + ulli[i].value + ',') != -1) ulli[i].checked = true;
    }
}

// 修改(插入)
function showResult() {
    var url = '/kobe/modifyRole.do?roleId=' + rId;
    $.get(url, respon);

    function respon(d) {
        console.log(d);
    }

    var roleName = $('#roleName').val();
    // console.log(roleName);
    var arr = [];
    $('input[type="checkbox"]:checked').each(function () {
        arr.push($(this).val());
    });
    var settings = {
        url: '/kobe/addRole.do?rName=' + roleName,
        method: 'post',
        headers: {
            'content-type': 'application/json'
        },
        data: JSON.stringify(arr)
    };
    $.ajax(settings).done(requestFinished1);
}

function requestFinished1(resp) {
    if (resp) {
        $('#save_result_info').attr('class', 'save_success').text('保存成功!');
        showResultDiv(true);
        window.setTimeout("showResultDiv(false);", 3000);
    } else {
        $('#save_result_info').attr('class', 'save_fail').text('保存失败!');
        showResultDiv(true);
        window.setTimeout("showResultDiv(false);", 3000);
    }
}

function showResultDiv(flag) {
    var divResult = document.getElementById("save_result_info");
    if (flag)
        divResult.style.display = "block";
    else
        divResult.style.display = "none";
}

function cancelResult() {
    $('#roleName').val('');
    $('input[type="checkbox"]:checked').attr("checked", false);
}
